<?php

/**
	*
  * $error = new Error();
	* $error->add(1, 'test error', FATAL_ERROR, false);
  * //$error->remove_error(1);
  * $error->display();
  *
	*/

define('FATAL_ERROR', 1);
define('NOTICE_ERROR', 2);
define('DEBUG_ERROR', 3);

class Error {
	/**
  	* stores all errors and notices
    *
		* @access private
		* @var 		string 
		*/
	var $errors = array();
  
  /**
    * This class method adds a new error to the errors' array
    *
    * @access   public
    * @author   Svilen
    *
    * @param    integer  $error_number   	Error number, should be unique
    * @param    string   $error_text     	Error text
    * @param    const		 $error_priority 	Error priority, e.g. FATAL_ERRRO, NOTICE_ERROR, DEBUG_ERROR
    * @param    boolean  $display_debug   If the error should be displayed
    *
    * @since    29-07-2005
    * @version  1.0
    *
    */
  function addError ($error_number, $error_text = '', $error_priority = FATAL_ERROR, $display_debug = false) {
  	// add error
    if (!empty($error_number)) {
    	$this->errors[] = array($error_number, $error_text, $error_priority);
    }
    
    // displays all errors which are set till here
  	if ($display_debug == true) {
    	$this->display();
    }
  }
  
  /**
    * This class method removes a specific error from the errors' array
    *
    * @access   public
    * @author   Svilen
    *
    * @param    integer  $error_number   	Error number, which should be removed
    *
    * @since    29-07-2005
    * @version  1.0
    *
    */
  function removeError($error_number) {
  	if (!empty($error_number)) {
      foreach ($this->errors as $key => $value) {
    		if ($value[0] == $error_number) {
   	    	unset ($this->errors[$key]);
          return true;
          break;
        }
      }
    }
    
    return false;
  }
  
  /**
    * This class method display a specific error or all set errors from the errors' array
    *
    * @access   public
    * @author   Svilen
    *
    * @param    integer  $which_error   	Error number, which should be displayed
    *
    * @since    29-07-2005
    * @version  1.0
    *
    */
  function display($which_error = null) {
  	$error_text = '';
    
  	if (!empty($this->errors)) {
  		foreach ($this->errors as $key => $value) {
      	// If we want only a specific error to be displayed
    		if (!empty($which_error)) {
        	if ($value[0] == $which_error) {
          	// Set necessary error message
         		$error_text .= $this->errorMessage($value[1], $value[2]);
          	break;
          }
          continue;
        }
        
        // Set error message
        $error_text .= $this->errorMessage($value[1], $value[2]);
      }
      
      // Displays the error(s)
      print $error_text;
    }
  }
  
  /**
    * This class method returns correct error message for a specific error
    *
    * @access   private
    * @author   Svilen
    *
    * @param    string   $message			Error text
    * @param    const		 $priority		Error priority, e.g. FATAL_ERRRO, NOTICE_ERROR, DEBUG_ERROR
    *
    * @since    29-07-2005
    * @version  1.0
    *
    */
  function errorMessage ($message, $priority) {
  	switch ($priority) {
    	case FATAL_ERROR:
      	return '<br /><b>Fatal Error</b>: '.$message;
      	break;
      case NOTICE_ERROR:
      	return '<br /><b>Notice</b>: '.$message;
      	break;
      case DEBUG_ERROR:
      	return '<br /><b>Debug</b>: '.$message;
      	break;
    }
  }
  
}

?>